﻿//#version 120
uniform sampler2D texture;
uniform int hasTexture=2;
uniform float uR = 0.5;     //球半径

varying vec3 col;
varying vec2 v_texcoord;
varying vec3 pos;
varying vec4 vDiffuse;      //漫反射
varying vec4 vSpecular;     //镜面反射
varying vec4 vAmbient;      //环境光

//! [0]
void main()
{
    vec4 flag;

    if(hasTexture==1)
    {
        flag = vec4(col, 1.0) * texture2D(texture, v_texcoord) * (vAmbient + vDiffuse + vSpecular);
    }
    else if(hasTexture==2){
        //棋盘贴图
        float n = 4.0;  //ܱ接立方体每个坐标轴方向分的数
        float span = 2.0 * uR/n;
        int i = int((pos.x + uR)/span);
        int j = int((pos.y + uR)/span);
        int k = int((pos.z + uR)/span);
        int wColor = int(mod(float(i+j+k), 2.0));
        if(wColor==1){
            //奇数
            flag = vec4(0.678, 0.231, 0.129, 1.0) * (vAmbient + vDiffuse + vSpecular);
        }
        else{
            flag = vec4(1.0, 1.0, 1.0, 1.0) * (vAmbient + vDiffuse + vSpecular);
        }
    }
    else{
        flag = vec4(col, 1.0) * (vAmbient + vDiffuse + vSpecular);
    }

    gl_FragColor = flag;

}
//! [0]
